﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using HaiFengShop.Service;
using HaiFengShop.Entity;

namespace XR.WebApi.Areas.HelpPage.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class CartController : ControllerBase
    {
        private readonly CartService  cartService;
        public CartController(CartService  cartService)
        {
            this.cartService = cartService;
        }

        /// <summary>
        /// 更新购物车的商品
        /// </summary>
        /// <param name="postData"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Update([FromBody]DTOPro postData)
        {
            return Ok(cartService.UpdateOrderInfo(postData.productId, postData.id, postData.number));
        }

        /// <summary>
        /// 更新购物车的商品
        /// </summary>
        /// <param name="postData"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Delete([FromBody]int productIds)
        {
            return Ok(cartService.DeleteOrderInfo(productIds));
        }

        /// <summary>
        /// 订单提交前的检验和填写相关订单信息
        /// </summary>
        /// <param name="addressId"></param>
        /// <param name="addType"></param>
        /// <param name="orderFrom"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Checkout(int addressId, int addType, int type, int orderFrom = 0)
        {
            return Ok(cartService.CheckOrderInfo(addressId, addType, orderFrom, type));
        }
        /// <summary>
        /// 添加商品到购物车
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add([FromBody]Dictionary<string, object> obj)
        {
            cartService.AddCartInfo(obj);

            DTOCart dc = cartService.GetCart(0);
            dynamic dd = new
            {
                cartList = dc.listCart,
                cartTotal = new
                {
                    goodsCount = dc.goodsCount,
                    goodsAmount = Math.Round(dc.goodsAmount.Value, 2, MidpointRounding.AwayFromZero),
                    checkedGoodsCount = dc.checkedGoodsCount,
                    checkedGoodsAmount = dc.checkedGoodsAmount,
                    user_id = dc.user_id,
                    numberChange = dc.numberChange
                }
            };

            ResponseMessage res = new ResponseMessage()
            {
                errno = 0,
                errmsg = "操作成功!",
                data = dd
            };

            return Ok(res);
        }


        /// <summary>
        /// 获取购物车信息，所有对购物车的增删改操作，都要重新返回购物车的信息
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index(int type = 0)
        {
            DTOCart dc = cartService.GetCart(type);
            dynamic dd = new
            {
                cartList = dc.listCart,
                cartTotal = new
                {
                    goodsCount = dc.goodsCount,
                    goodsAmount = Math.Round(dc.goodsAmount.Value, 2, MidpointRounding.AwayFromZero),
                    checkedGoodsCount = dc.checkedGoodsCount,
                    checkedGoodsAmount = dc.checkedGoodsAmount,
                    user_id = dc.user_id,
                    numberChange = dc.numberChange
                }
            };

            ResponseMessage res = new ResponseMessage()
            {
                errno = 0,
                errmsg = "操作成功!",
                data = dd
            };

            return Ok(res);
        }

        /// <summary>
        /// 获取购物车商品的总件件数
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GoodsCount(int type = 0)
        {

            return Ok(cartService.GetGoodsCartCount(type));
        }
        /// <summary>
        /// 是否选择商品，如果已经选择，则取消选择，批量操作
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Checked([FromBody]DTOCheckedData postData)
        {
            cartService.CheckedData(postData);

            DTOCart dc = cartService.GetCart(0);
            dynamic dd = new
            {
                cartList = dc.listCart,
                cartTotal = new
                {
                    goodsCount = dc.goodsCount,
                    goodsAmount = Math.Round(dc.goodsAmount.Value, 2, MidpointRounding.AwayFromZero),
                    checkedGoodsCount = dc.checkedGoodsCount,
                    checkedGoodsAmount = dc.checkedGoodsAmount,
                    user_id = dc.user_id,
                    numberChange = dc.numberChange
                }
            };

            ResponseMessage res = new ResponseMessage()
            {
                errno = 0,
                errmsg = "操作成功!",
                data = dd
            };

            return Ok(res);
        }
    
    }
}
